package com.bajie.bjlisten.sevice;


import com.alibaba.fastjson.JSONObject;
import com.bajie.bjlisten.entity.*;
import com.bajie.bjlisten.entity.response.BjUserCode;
import com.bajie.bjlisten.entity.response.CommonCode;
import com.bajie.bjlisten.entity.response.EBookResult;
import com.bajie.bjlisten.entity.response.EBookShelfResult;
import com.bajie.bjlisten.mapper.BjEBookMapper;
import com.bajie.bjlisten.mapper.BjEBookShelfMapper;
import com.bajie.bjlisten.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.transaction.Transactional;
import java.util.*;

@Slf4j
@Service
@Transactional
public class EBookService {
    @Autowired
    BjEBookShelfMapper bjEBookShelfMapper;
    @Autowired
    BjUserService bjUserService;
    @Autowired
    BjEBookMapper bjEBookMapper;
    @Autowired
    BjCategoryService bjCategoryService;
    @Autowired
    BjChaptersService bjChaptersService;

    /**
     * 根据用户和小说ID删除书架
     * @param userId
     * @param ebookId
     * @return
     */
    public EBookShelfResult deleteByEbookIdAndUserId(String userId, String ebookId) {
        BjEBookShelf bjEBookShelf;
        bjEBookShelf= bjEBookShelfMapper.findByEbookIdAndUserId(ebookId, userId);
        if (null==bjEBookShelf)
            return new EBookShelfResult(BjUserCode.BJEBOOKSHELF_SELECT_ORROR);
        bjEBookShelfMapper.deleteByEbookIdAndUserId(userId, ebookId);
        bjEBookShelf = bjEBookShelfMapper.findByEbookIdAndUserId(ebookId, userId);
        if (null!=bjEBookShelf) {//查询为空，标识删除成功
            return new EBookShelfResult(BjUserCode.BJEBOOKSHELF_GET_FAIL);
        }

        return new EBookShelfResult(CommonCode.SUCCESS);

    }


    //前台传用户ID和小说ID加入书架

    /**
     * 先判断用户id是否授权绑定；
     * 再根据小说ID判断是否存在于小说表中；
     * 再次根据小说ID和用户ID插入到书架表中
     */
    public EBookShelfResult putIntoBookShelf(String userId, String ebookId) {
        //根据用户Id和小说ID查询是否存在书架表里
        BjEBookShelf bjEBookShelf = bjEBookShelfMapper.findByEbookIdAndUserId(userId, ebookId);
        if (null != bjEBookShelf) {
            //存在书架里面
            return new EBookShelfResult(BjUserCode.BJEBOOKSHELF_ERROR);
        }
        BjUser bjUser = bjUserService.findByUserId(userId);
        if (StringUtils.isNotEmpty(bjUser.getOpenid())) {//授权过的用户Id
            BjEBook bjEBook = bjEBookMapper.getById(ebookId);
            if (null != bjEBook) {
                bjEBookShelf=new BjEBookShelf();
                bjEBookShelf.setBook_img(bjEBook.getBookpic()==null?"":bjEBook.getBookpic());
                bjEBookShelf.setEbook_name(bjEBook.getEbookname());
                bjEBookShelf.setPopularity(bjEBook.getPopularity());
                bjEBookShelf.setSynopsis(bjEBook.getSynopsis());
                bjEBookShelf.setWriter(bjEBook.getWriter());
                bjEBookShelf.setWriterId(bjEBook.getWriterId());
                bjEBookShelf.setEbook_id(ebookId);
                bjEBookShelf.setUser_id(userId);
                bjEBookShelfMapper.saveAndFlush(bjEBookShelf);
                return new EBookShelfResult(CommonCode.SUCCESS, bjEBookShelf);


            }
        }
        return new EBookShelfResult(BjUserCode.BJEBOOKSHELF_ERROR);

    }

    /**
     * 根据用户查询书架小说信息
     * @param userId
     * @return
     */
    public EBookResult findBookShelfsByUserId(String userId,Integer offset, Integer limit){
        //获取小说ID信息集合
        Set<String> bookIdsbyShelf= findBookShelfByUserId(userId);
        Set<BjEBook> bjEBooks=new HashSet<>();
        if (StringUtils.isNotEmpty(bookIdsbyShelf)){
            bjEBooks= findAllByBookIds(bookIdsbyShelf,offset,limit);
        }

        List<Object> listAll=new ArrayList<>();
        List<Object> eBookInfo;
        if (StringUtils.isNotEmpty(bjEBooks)){
            for (BjEBook bjEBook:bjEBooks){
                eBookInfo=new ArrayList<>();
                JSONObject jsonEbook = new JSONObject();//存放小说/分类
                jsonEbook.put("ebookId",bjEBook.getId());
                jsonEbook.put("ebookname",bjEBook.getEbookname());
                jsonEbook.put("ebookPic",bjEBook.getBookpic());
                jsonEbook.put("ebookId",bjEBook.getId());
                jsonEbook.put("writer",bjEBook.getWriter());
                jsonEbook.put("isSerialize",bjEBook.getIsSerialize());
                jsonEbook.put("synopsis",bjEBook.getSynopsis());
                jsonEbook.put("popularity",bjEBook.getPopularity());
                eBookInfo.add(jsonEbook);
                listAll.addAll(eBookInfo);
            }
            return  new EBookResult(CommonCode.SUCCESS,listAll);

        }

        //根据小说ID集合结合小说表查询小说表集合，再获取小说分类表集合
        //根据小说分类集合结合小说分类表查询小说的分类信息

        return  new EBookResult( BjUserCode.BJEBOOKSHELF_FAIL);

    }

    /**
     * 根据用户ID查询加入书架的小说ID集合
     * @param userId
     * @return
     */
    public Set<String> findBookShelfByUserId(String userId){
        Set<String> setBookId=new HashSet<>();//存放小说ID集合
        Set<BjEBookShelf> bjEBookShelves=
                bjEBookShelfMapper.findBookShelfByUserId(userId);
        if (StringUtils.isNotEmpty(bjEBookShelves)&&bjEBookShelves.size()>0){
            for (BjEBookShelf bookShelf:bjEBookShelves){
                setBookId.add(bookShelf.getEbook_id());
            }
        }
        return  setBookId;
    }


    /**
     * 猜你喜欢
     * @param userId
     * @param offset
     * @param limit
     * @return
     */

    public EBookResult findAllGuess(String userId, Integer offset, Integer limit){

        List<Object> listAll;

        //存放用户喜欢的分类ID
        Set<String> categoryIdSet=new HashSet<>();//存放用户喜欢的分类Id
        //存放用户喜欢的偏好信息集合 TODO
        Set<BjCategory> bjCategorySet;
        bjCategorySet=bjCategoryService.findUserInterestBjCategoryName(userId, offset, limit);
        if(StringUtils.isNotEmpty(bjCategorySet)||bjCategorySet.size()>0){  //用户偏好信息不为空
           for(BjCategory category:bjCategorySet){
                       categoryIdSet.add(category.getId());
            }

        }else {
            //当用户没有偏好的分类时，随机获取小说分类
            bjCategorySet= bjCategoryService.selectRandomCategory(limit);
            if(StringUtils.isNotEmpty(bjCategorySet)){
                for(BjCategory category:bjCategorySet){
                    categoryIdSet.add(category.getId());
                }

            }

        }

        Set<BjEBook> bjEBooks=new HashSet<>();//存放小说信息的集合 TODO
        Set<String> ebookIdSet=new HashSet<>();//存放小说ID集合
        if (StringUtils.isNotEmpty(categoryIdSet)||categoryIdSet.size()>0){
            //根据用户喜欢的分类ID集合结合小说表查询需要数量的小说
            bjEBooks=bjEBookMapper.findAllByCategorys(categoryIdSet,offset,limit);

        }

        if(StringUtils.isNotEmpty(bjEBooks)||bjEBooks.size()>0){
            for (BjEBook bjEBook : bjEBooks) {
                ebookIdSet.add(bjEBook.getId());

            }
        }
        //根据小说集合查询章节：章节ID，简介、最新章节标题、最新章节的更新时间和总字数，排序，更新时间，总字数
        Set<BjChapters> bjChaptersSet=new HashSet<>(); //TODO
        //根据小说ID集合查询章节信息
        //章节ID，简介、最新章节标题、最新章节的更新时间和总字数，排序，更新时间，总字数
        if (StringUtils.isNotEmpty(ebookIdSet)||ebookIdSet.size()>0){
            bjChaptersSet= bjChaptersService.findChapterByEbookIds(ebookIdSet,offset,limit);

        }

        //判断章节集合是否存在
        if(StringUtils.isNotEmpty(bjChaptersSet)){
            //根据用户ID查询加入书架的小说ID集合 TODO
            Set<String> bookIdsbyShelf= findBookShelfByUserId(userId);
            listAll=commontEbooks(bjEBooks,bookIdsbyShelf,bjChaptersSet,bjCategorySet);
            return  new EBookResult(CommonCode.SUCCESS,listAll);



        }

        return  new EBookResult( BjUserCode.BJEBOOK_GET_FAIL);

    }


    /**
     * 八戒热推
     * @param userId
     * @param offset
     * @param limit
     * @return
     */


    public EBookResult findAllByPop(String userId,Integer offset,Integer limit){
        List<Object> listAll;

        //按照人气值查询小说表组成小说集合set
        Set<BjEBook> bjEBooks=bjEBookMapper.findAllByPop(offset, limit);
        Set<String> bookIds=new HashSet<>();//存放小说ID集合
        Set<String> categoryIds=new HashSet<>();//存放分类ID集合
        if (StringUtils.isNotEmpty(bjEBooks)){
            //根据小说集合set提取小说ID集合和分类ID集合
            for (BjEBook bjEBook:bjEBooks){
                bookIds.add(bjEBook.getId());
                categoryIds.add(bjEBook.getMt());
            }
        }
        //通过小说分类ID集合结合小说分类表查询小说分类信息{分类ID，分类名称}
        Set<BjCategory> bjCategorySet=new HashSet<>();//存放分类信息
        if (StringUtils.isNotEmpty(categoryIds)){
            bjCategorySet=bjCategoryService.findListBjCategory(categoryIds);
        }
        Set<BjChapters> bjChaptersSet=new HashSet<>();
        if (StringUtils.isNotEmpty(bookIds)){
            //根据小说集合结合小说章节表查询小说章节的章节信息{最新章节的ID，最新章节的名称，最新章节的更新时间，字数}
            bjChaptersSet= bjChaptersService.findChapterByEbookIds(bookIds,offset,limit);
        }
        //判定章节不为空
        if(StringUtils.isNotEmpty(bjChaptersSet)){
            Set<String> bookIdsbyShelf= findBookShelfByUserId(userId);//存放该用户放入书架的小说ID集合

            listAll=commontEbooks(bjEBooks,bookIdsbyShelf,bjChaptersSet,bjCategorySet);
            return  new EBookResult(CommonCode.SUCCESS,listAll);
        }

        //根据用户ID结合书架表查询存在于书架表的小说ID的集合
        return  new EBookResult( BjUserCode.BJEBOOK_GET_FAIL);

    }

    /**
     * 猜你喜欢和八戒热推共同方法，提取出来了
     * @param bjEBooks
     * @param bookIdsbyShelf
     * @param bjChaptersSet
     * @param bjCategorySet
     * @return
     */
    public List<Object> commontEbooks(Set<BjEBook> bjEBooks,Set<String> bookIdsbyShelf,Set<BjChapters> bjChaptersSet,Set<BjCategory> bjCategorySet) {

            List<Object> listAll=new ArrayList<>();
            List<Object> eBookInfo;
            for (BjEBook bjEBook:bjEBooks){
                JSONObject jsonEbook = new JSONObject();//存放小说/分类/最新章节
                eBookInfo=new ArrayList<>();//存放小说/分类/最新章节
                jsonEbook.put("ebookId",bjEBook.getId());
                jsonEbook.put("ebookName",bjEBook.getEbookname());
                jsonEbook.put("ebookPic",bjEBook.getBookpic());
                jsonEbook.put("writer",bjEBook.getWriter());
                jsonEbook.put("isSerialize",bjEBook.getIsSerialize());
                jsonEbook.put("synopsis",bjEBook.getSynopsis());
                jsonEbook.put("popularity",bjEBook.getPopularity());
                // TODO 是否加入书架
                if (StringUtils.isNotEmpty(bookIdsbyShelf)){
                    if(bookIdsbyShelf.contains(bjEBook.getId()))
                        jsonEbook.put("isBookShef","已加入书架");
                    else
                        jsonEbook.put("isBookShef","未加入书架");


                }else
                    jsonEbook.put("isBookShef","未加入书架");

                for (BjChapters chapters: bjChaptersSet){

                    System.out.println("小说表的小说ID为"+bjEBook.getId());
                    System.out.println("章节表的小说ID为"+chapters.getEbookId());
                    System.out.println(StringUtils.isEqual(chapters.getEbookId(),bjEBook.getId()));

                    if (StringUtils.isEqual(chapters.getEbookId(),bjEBook.getId())){
                        jsonEbook.put("newChapterId",chapters.getId());
                        jsonEbook.put("newChapterName",chapters.getChaptername());
                        jsonEbook.put("updateTime",chapters.getUpdateTime());
                        jsonEbook.put("wordSize",chapters.getWordSize());
                        //continue;
                        break;
                    }

                }
                for(BjCategory category:bjCategorySet){
                    System.out.println("小说表的分类ID为"+bjEBook.getMt());
                    System.out.println("分类表的分类ID为"+category.getId());
                    if (StringUtils.isEqual(category.getId(),bjEBook.getMt())) {
                        System.out.println(StringUtils.isEqual(category.getId(),bjEBook.getMt()));
                        jsonEbook.put("categoryId",category.getId());
                        jsonEbook.put("categoryName",category.getName());
                        //continue;
                        break;
                    }

                }
                eBookInfo.add(jsonEbook);
                listAll.addAll(eBookInfo);

            }


        return listAll;
    }

    /**
     * 连载更新
     */
    public EBookResult findBookByupdateTime(String userId,Integer offset,Integer limit){
        List<Object> listAll;

        //按照小说最后的更新时间排序获取章节信息
        Set<BjChapters> bjChaptersSet=bjChaptersService.findByChapterTime(offset, limit);
        Set<String> ebookIds=new HashSet<>();//存放小说ID集合
        if(StringUtils.isNotEmpty(bjChaptersSet)){
            for (BjChapters chapter:bjChaptersSet)
            ebookIds.add(chapter.getEbookId()); //获取到的小说ID集合
        }
        //根据小说ID集合结合小说表查询小说信息
        Set<BjEBook> bjEBookSet= findAllByBookIds(ebookIds,offset,limit);
        Set<String> categoryIds=new HashSet<>();//存放分类ID集合
        if (StringUtils.isNotEmpty(bjEBookSet)){
            for (BjEBook bjEBook:bjEBookSet){
                categoryIds.add(bjEBook.getMt());//章节ID集合
            }
        }
        Set<BjCategory> bjCategorySet=bjCategoryService.findListBjCategory(categoryIds);
        if (StringUtils.isNotEmpty(bjCategorySet)){
            Set<String> bookIdsbyShelf= findBookShelfByUserId(userId);//存放该用户放入书架的小说ID集合
            listAll=commontEbooks(bjEBookSet,bookIdsbyShelf,bjChaptersSet,bjCategorySet);
            return  new EBookResult(CommonCode.SUCCESS,listAll);

        }

        return  new EBookResult( BjUserCode.BJEBOOK_GET_FAIL);

    }

    /**
     * 根据小说ID集合查询要求数量的小说信息
     * @param ebookIds
     * @param offset
     * @param limit
     * @return
     */
    public Set<BjEBook> findAllByBookIds(Set<String> ebookIds,Integer offset,Integer limit){
        return  bjEBookMapper.findAllByBookIds(ebookIds, offset, limit);

    }

    /**
     * 点击一次 增加人气值+1
     * @param ebookId
     * @return
     */
    @Transactional
    public int addPop(String ebookId){
        return bjEBookMapper.addPop(ebookId);
    }

    /**
     * 根据用户Id和小说Id查询书架详情
     * @param user_id
     * @param ebook_id
     * @return
     */
    public BjEBookShelf findByEbookIdAndUserId(String user_id,String ebook_id){
        if(StringUtils.isNotEmpty(user_id)&&StringUtils.isNotEmpty(ebook_id)){
            return bjEBookShelfMapper.findByEbookIdAndUserId(user_id, ebook_id);
        }
        return null;
    }

    /**
     * 根据小说ID查询书籍详情
     */

    /**
     * 根据小说ID集合查询小说表详情
     * @param id
     * @return
     */
    public BjEBook getById(String id){
        return bjEBookMapper.getById(id);
    }

    /**
     * 根据小说Id查询小说详情
     * @param userId
     * @param ebookId
     * @return
     */
    public EBookResult findByBookId(String userId,String ebookId){
        BjEbook_ChaptersDto bjEbook_chaptersDto=new BjEbook_ChaptersDto();
        //根据小说ID集合查询小说表详情
        BjEBook bjEBook=getById(ebookId);
        String categoryId=null;
        if (null!=bjEBook){
            categoryId=bjEBook.getMt();
            bjEbook_chaptersDto.setCategoryId(bjEBook.getMt());
            bjEbook_chaptersDto.setPopularity(bjEBook.getPopularity());
            bjEbook_chaptersDto.setSynopsis(bjEBook.getSynopsis());
            bjEbook_chaptersDto.setIsSerialize(bjEBook.getIsSerialize().getDesc());
            bjEbook_chaptersDto.setWriter(bjEBook.getWriter());
            bjEbook_chaptersDto.setBookpic(bjEBook.getBookpic());
            bjEbook_chaptersDto.setEbookname(bjEBook.getEbookname());
            bjEbook_chaptersDto.setWriterId(bjEBook.getWriterId());
        }
        //根据小说表分类ID查询小说分类
        BjCategory bjCategory=bjCategoryService.getById(categoryId);
        if (null!=bjCategory){
            bjEbook_chaptersDto.setCategoryName(bjCategory.getName());
        }
        //根据用户ID和小说ID查询是否存在书架里面
        BjEBookShelf bjEBookShelf=findByEbookIdAndUserId(userId,ebookId);
        if (null !=bjEBookShelf){
            bjEbook_chaptersDto.setIsBookShef("已加入书架");
        }else{
            bjEbook_chaptersDto.setIsBookShef("未加入书架");
            }

        //根据小说ID查询章节信息
        List<BjChapters> bjChaptersList=bjChaptersService.selectChapterNameByEBookId(ebookId);

        if (null!=bjChaptersList&&bjChaptersList.size()>0){
            bjEbook_chaptersDto.setBjChaptersList(bjChaptersList);
            return  new EBookResult(CommonCode.SUCCESS,bjEbook_chaptersDto);
        }

        return  new EBookResult( BjUserCode.BJEBOOK_GET_FAIL);
    }


    /**
     * 根据小说ID查询目录章节
     * @param ebookId
     * @return
     */
    public EBookShelfResult findChapterByEBookId(String ebookId){
        List<BjChapters> bjChaptersList
                =bjChaptersService.selectChapterNameByEBookId(ebookId);
        if (StringUtils.isNotEmpty(bjChaptersList)&&bjChaptersList.size()>0){

            return  new EBookShelfResult(CommonCode.SUCCESS,bjChaptersList);
        }
        return  new EBookShelfResult( BjUserCode.BJEBOOK_GET_FAIL);
    }



    /**
     * 同类推荐
     * 根据分类ID随机查询5本小说表小说详情
     * @param categoryId
     * @param offset
     * @param limit
     * @return
     */
    public EBookResult findCategoryByRandBookId(String categoryId,Integer offset,Integer limit){
        List<Object> list=new ArrayList<>();
        //根据分类ID查询小说分类信息
        BjCategory bjCategory=bjCategoryService.getById(categoryId);
        Set<String> set=new HashSet<>();//存放小说分类ID
        JSONObject jsonCategory=new JSONObject();//存放小说分类信息的json

        JSONObject jsonEbook=new JSONObject();//存放小说信息的json

        Set<BjEBook>  bjEBooks=new HashSet<>();//查询结果存放小说信息集合
        set.add(categoryId);
        if (null!=bjCategory){
            jsonCategory.put("categoryId",bjCategory.getId());
            bjCategory.getName();
            jsonCategory.put("categoryName",bjCategory.getName());
            bjEBooks=bjEBookMapper.findAllByCategorys(set,offset,limit);

        }
        if (StringUtils.isNotEmpty(bjEBooks)){
            for (BjEBook bjEBook:bjEBooks){
                jsonEbook=new JSONObject();
                jsonEbook.put("ebookId",bjEBook.getId());
                jsonEbook.put("writerId",bjEBook.getWriterId());
                jsonEbook.put("writer",bjEBook.getWriter());
                jsonEbook.put("bookpic",bjEBook.getBookpic());
                jsonEbook.put("iserialize",bjEBook.getIsSerialize().getDesc());
                jsonEbook.put("popularity",bjEBook.getPopularity());
                jsonEbook.put("categoryId",bjEBook.getMt());
                jsonEbook.put("categoryName",bjCategory.getName());
                list.add(jsonEbook);
            }
            return  new EBookResult(CommonCode.SUCCESS,list);
        }
        //根据分类ID随机查询5本小说表小说详情
        return  new EBookResult( BjUserCode.BJEBOOK_GET_FAIL);

    }

    /*





    //获取小说相关信息
    public EBookResult getEBook() {
        //小说相关集合
        List<BjEBook> all = eBookRepository.findAll();
        //返回状态码及相关集合
        return new EBookResult(CommonCode.SUCCESS, all);
    }
    //根据章节id查询
    public List getChapters(String eBookId) {
        //根据id查询所有相关信息
        List<BjChapters> bjChapters = bjChaptersMapper.selectChapterNameByEBookId(eBookId);
        //创建集合
        List<String> list = new ArrayList<>();
        //遍历
        for (BjChapters bjchapter:bjChapters) {
            if (StringUtil.isNotEmpty(bjchapter.getChapterName())){
                list.add(bjchapter.getChapterName());
            }
        }
        //返回集合
        return list;
    }

    //根据章节id获取该章节内容
    public JSONObject getContent(String chapterId) {

        //根据章节id获取内容
        List<BjChapters> bjChapters = bjChaptersMapper.selectContentByChapterId(chapterId);

        StringBuffer sb = new StringBuffer("");
        //创建字符串
        String s=null;
        boolean okstatus=false;
        //遍历及展出
        String chapterName=null;
        for (BjChapters bjChapters1:bjChapters){
            if (StringUtil.isNotEmpty(bjChapters1.getChapterContent())){
                s = bjChapters1.getChapterContent();
                chapterName=bjChapters1.getChapterName();
                sb.append(s);
                //return s;
            }
        }
        JSONObject jsonAll = new JSONObject();
        JSONObject json = new JSONObject();
        if(null!=sb&&StringUtils.isNotEmpty(sb.toString())){
            okstatus=true;
        }
        jsonAll.put("OK",okstatus);
        json.put("title",chapterName);
        json.put("body",s);
        jsonAll.put("chapter",json);


        return jsonAll;
        //return "error ，无内容";
    }

    //根据小说id获取章节id及章节名
    public Map<String, Object> getChapterIdAndChapterName(String eBookId) {

        List<BjChapters> bjChapters = bjChaptersMapper.selectChapterIdAndChapterNameByEBookId(eBookId);

        //创建map集合存储map数据
        Map<String,Object> map = new HashMap<>();

        for (BjChapters bjchapter:bjChapters) {
            if (StringUtil.isNotEmpty(bjchapter.getId())&& StringUtil.isNotEmpty(bjchapter.getChapterName())){
                map.put(bjchapter.getId(),bjchapter.getChapterName());
            }
        }

        return map;
    }

    //根据用户点击小说，返回该小说相关信息
    public Map<String,Object> getEbookContent(String eBookId) {

        List<BjEBook> all = eBookRepository.findAllById(eBookId);

        //创建map集合
        Map<String,Object> map = new HashMap<>();

        //遍历及添加map
        for (BjEBook bjeBook:all) {
            map.put("小说id", bjeBook.getId());
            map.put("书名", bjeBook.getEbookname());
            map.put("作者", bjeBook.getWriter());
            map.put("简介", bjeBook.getSynopsis());
            map.put("人气", bjeBook.getPopularity());
            map.put("小说图片", bjeBook.getBookPic());
            map.put("更新时间",bjeBook.getCreateTime());
        }
        return map;
    }

    //随机推荐
    public List getRandomNovels(Integer num) {

        //创建集合
        List<BjEBook> list = bjEBookMapper.selectRandomEBook(num);

//        List<BjEBook> arrayList = new ArrayList<>();

        for (BjEBook bjEBook:list) {

        }

        //返回list集合
        return list;
    }

    //八戒热推
    public List getBjHotNovels(Integer bjNum) {

        //根据输入数字返回多个书籍（根据人气排名）
        List<BjEBook> list = bjEBookMapper.selectBjHotEBook(bjNum);

        for (BjEBook bjEBook:list) {

        }
        //返回热推集合
        return list;
    }

    //加入书架
    public EBookShelfResult putIntoBookShelf(String userId,String ebookId) {

        try {
            //点击书籍列表图书，添加书架
            List<BjEBook> allById = eBookRepository.findAllById(ebookId);

            //创建对象
            BjEBookShelf byEbookId = new BjEBookShelf();

            //遍历
            for (BjEBook bjEBook:allById){
                if (bjEBook.getId().isEmpty()){
                    return new EBookShelfResult(EBookCode.EBOOK_ISNULLERROR);
                }
//                if (bjEBook.getId().equals(ebookId)) {
//                    return new EBookShelfResult(EBookCode.EBOOK_ISEXISTERROR);
//                }
                //补全属性
                byEbookId.setEbookId(bjEBook.getId());
                byEbookId.setEbookName(bjEBook.getEbookname());
                byEbookId.setWriter(bjEBook.getWriter());
                byEbookId.setSynopsis(bjEBook.getSynopsis());
                byEbookId.setPopularity(bjEBook.getPopularity());
                byEbookId.setEbookImg(bjEBook.getBookPic());
                byEbookId.setUserId(userId);

                //保存属性
                bjEBookShelfRepository.save(byEbookId);
            }

            return new EBookShelfResult(CommonCode.SUCCESS,byEbookId);
        }catch (Exception e){
//            ExceptionCast.cast(CommonCode.FAIL);
            e.printStackTrace();
        }

        return new EBookShelfResult(CommonCode.FAIL);
    }


    public Boolean deleteByEbookIdAndUserId(String userId,String ebookId){

        bjEBookShelfRepository.deleteByEbookIdAndUserId(userId,ebookId);
        BjEBookShelf bjEBookShelf=bjEBookShelfRepository.findByEbookIdAndUserId(ebookId,userId);
        if(null!=bjEBookShelf.getId()){
            return true;
        }
        return false;

    }*/
}
